<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Reduces the vector to a single value using a callback function</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="ds-vector.push.html">« Ds\Vector::push</a></li>
      <li style="float: right;"><a href="ds-vector.remove.html">Ds\Vector::remove »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.ds-vector.html">Vector</a></li>
    <li>Reduces the vector to a single value using a callback function</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="ds-vector.reduce" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">Ds\Vector::reduce</h1>
  <p class="verinfo">(PECL ds &gt;= 1.0.0)</p><p class="refpurpose"><span class="refname">Ds\Vector::reduce</span> &mdash; <span class="dc-title">Reduces the vector to a single value using a callback function</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-ds-vector.reduce-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>Ds\Vector::reduce</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$callback</code></span>, <span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$initial</code><span class="initializer"> = ?</span></span>): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>

  <p class="para rdfs-comment">
    Reduces the vector to a single value using a callback function.
  </p>

 </div>


 <div class="refsect1 parameters" id="refsect1-ds-vector.reduce-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
    <dt>
<code class="parameter">callback</code></dt>


    <dd>

      <div class="methodsynopsis dc-description"><span class="methodname"><span class="replaceable">callback</span></span>(<span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$carry</code></span>, <span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$value</code></span>): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>

      <dl>
       
        <dt>
<code class="parameter">carry</code></dt>

        <dd>

         <p class="para">
          The return value of the previous callback, or <code class="parameter">initial</code> if
          it&#039;s the first iteration.
         </p>
        </dd>

       
       
        <dt>
<code class="parameter">value</code></dt>

        <dd>

         <p class="para">
          The value of the current iteration.
         </p>
        </dd>

       
      </dl>

     </dd>


   
   
    <dt>
<code class="parameter">initial</code></dt>

    <dd>

     <p class="para">
        The initial value of the carry value. Can be <strong><code>null</code></strong>.
     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-ds-vector.reduce-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
    The return value of the final callback.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-ds-vector.reduce-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-4015">
   <p><strong>示例 #1 <span class="function"><strong>Ds\Vector::reduce()</strong></span> with initial value example</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$vector&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;\</span><span style="color: #0000BB">Ds</span><span style="color: #007700">\</span><span style="color: #0000BB">Vector</span><span style="color: #007700">([</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">]);<br /><br /></span><span style="color: #0000BB">$callback&nbsp;</span><span style="color: #007700">=&nbsp;function(</span><span style="color: #0000BB">$carry</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$carry&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">;<br />};<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$vector</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">reduce</span><span style="color: #007700">(</span><span style="color: #0000BB">$callback</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">//&nbsp;Iterations:<br />//<br />//&nbsp;$carry&nbsp;=&nbsp;$initial&nbsp;=&nbsp;5<br />//<br />//&nbsp;$carry&nbsp;=&nbsp;$carry&nbsp;*&nbsp;1&nbsp;=&nbsp;&nbsp;5<br />//&nbsp;$carry&nbsp;=&nbsp;$carry&nbsp;*&nbsp;2&nbsp;=&nbsp;10<br />//&nbsp;$carry&nbsp;=&nbsp;$carry&nbsp;*&nbsp;3&nbsp;=&nbsp;30<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
int(30)
</pre></div>
   </div>
  </div>
  <div class="example" id="example-4016">
   <p><strong>示例 #2 <span class="function"><strong>Ds\Vector::reduce()</strong></span> without an initial value example</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$vector&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;\</span><span style="color: #0000BB">Ds</span><span style="color: #007700">\</span><span style="color: #0000BB">Vector</span><span style="color: #007700">([</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">]);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$vector</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">reduce</span><span style="color: #007700">(function(</span><span style="color: #0000BB">$carry</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$carry&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #0000BB">$value&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">;<br />}));<br /><br /></span><span style="color: #FF8000">//&nbsp;Iterations:<br />//<br />//&nbsp;$carry&nbsp;=&nbsp;$initial&nbsp;=&nbsp;null<br />//<br />//&nbsp;$carry&nbsp;=&nbsp;$carry&nbsp;+&nbsp;1&nbsp;+&nbsp;5&nbsp;=&nbsp;&nbsp;6<br />//&nbsp;$carry&nbsp;=&nbsp;$carry&nbsp;+&nbsp;2&nbsp;+&nbsp;5&nbsp;=&nbsp;13<br />//&nbsp;$carry&nbsp;=&nbsp;$carry&nbsp;+&nbsp;3&nbsp;+&nbsp;5&nbsp;=&nbsp;21<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
int(21)
</pre></div>
   </div>
  </div>
 </div>



</div></div></div></body></html>